using System;
using UnityEngine;

public class Challenge_item : MonoBehaviour
{
	public int ID;

	public UIEventListener picbtn;

	public UIEventListener helpbtn;

	public UILabel timelabel;

	public UILabel numlabel;

	public TweenRotation animaku;

	public UIEventListener closebtn;

	public UIWidget area1wi;

	public UIWidget area2wi;

	public Transform midtra;

	public UIEventListener[] modebtn;

	public UIWidget[] lockwi;

	public UIWidget[] unlockwi;

	public UILabel[] energylabel;

	public UILabel[] locklabel;

	public UIWidget forlockwi;

	public UIWidget forchancewi;

	public UILabel forlockinfo;

	public DateSave_Challenge ds_challenge = default(DateSave_Challenge);

	public ChallengeData[] challdata;

	private int curlevel;

	private int intervaltime;

	private int chancetime = 14400;

	private bool showtime;

	public UISprite forhint;

	public UISprite[] backhint;

	private int hintindex;

	private float caltime;

	private bool islostfocus;

	private void Start()
	{
		picbtn.onClick = ClickPicBtn;
		helpbtn.onClick = ClickhelpBtn;
		closebtn.onClick = ClickcloseBtn;
		for (int i = 0; i < 3; i++)
		{
			modebtn[i].onClick = clickmodeBtn;
		}
	}

	public bool enable()
	{
		bool flag = false;
		Vector3 eulerAngles = animaku.transform.localRotation.eulerAngles;
		if (eulerAngles.y != 0f)
		{
			animaku.ResetToBeginning();
			animaku.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
		}
		showarea1();
		ds_challenge = MenuPlayerprefs.instance.DS_Challenge[ID - 6];
		curlevel = MenuPlayerprefs.instance.curgamelevel;
		switch (ID)
		{
			case 6:
				for (int l = 0; l < 3; l++)
				{
					challdata[l] = MenuDataController.instance.surdata[l].challdata;
					hintindex = 0;
				}
				break;
			case 7:
				for (int j = 0; j < 3; j++)
				{
					challdata[j] = MenuDataController.instance.tdmdata[j].challdata;
					hintindex = 3;
				}
				break;
			case 8:
				for (int k = 0; k < 3; k++)
				{
					challdata[k] = MenuDataController.instance.dtbdata[k].challdata;
					hintindex = 6;
				}
				break;
			case 9:
				for (int i = 0; i < 3; i++)
				{
					challdata[i] = MenuDataController.instance.ffadata[i].challdata;
					hintindex = 9;
				}
				break;
		}
		if (curlevel >= challdata[0].openlevel)
		{
			forchancewi.alpha = 1f;
			forchancewi.gameObject.SetActive(true);
			forlockwi.alpha = 0f;
			forlockwi.gameObject.SetActive(false);
			showtimeinfo();
		}
		else
		{
			forchancewi.alpha = 0f;
			forchancewi.gameObject.SetActive(false);
			forlockwi.alpha = 1f;
			forlockwi.gameObject.SetActive(true);
			forlockinfo.text = "通过 lv" + ((challdata[0].openlevel - 1) / 6 + 1) + "-" + ((challdata[0].openlevel - 1) % 6 + 1) + " 解锁";
		}
		for (int m = 0; m < 3; m++)
		{
			if (curlevel >= challdata[m].openlevel)
			{
				unlockwi[m].alpha = 1f;
				unlockwi[m].gameObject.SetActive(true);
				lockwi[m].alpha = 0f;
				lockwi[m].gameObject.SetActive(false);
				energylabel[m].text = string.Empty + challdata[m].energy;
				if (MenuPlayerprefs.instance.challengehint[hintindex + m] == 0)
				{
					backhint[m].enabled = true;
					flag = true;
				}
				else
				{
					backhint[m].enabled = false;
					flag = false;
				}
			}
			else
			{
				unlockwi[m].alpha = 0f;
				unlockwi[m].gameObject.SetActive(false);
				lockwi[m].alpha = 1f;
				lockwi[m].gameObject.SetActive(true);
				locklabel[m].text = "通过 lv" + ((challdata[m].openlevel - 1) / 6 + 1) + "-" + ((challdata[m].openlevel - 1) % 6 + 1) + " 解锁";
			}
		}
		if (flag)
		{
			forhint.enabled = true;
		}
		else
		{
			forhint.enabled = false;
		}
		return flag;
	}

	public void showtimeinfo()
	{
		showtime = false;
		if (ds_challenge.challengenum >= 3)
		{
			timelabel.text = "挑战次数";
			numlabel.text = "3/3";
			return;
		}
		long num = 0L;
		num = ((!(ds_challenge.challengetime == string.Empty)) ? long.Parse(ds_challenge.challengetime) : DateTime.Now.Ticks);
		long ticks = DateTime.Now.Ticks;
		long num2 = ticks - num;
		if (num2 > 0)
		{
			intervaltime = (int)(num2 / 10000000);
		}
		else
		{
			intervaltime = 0;
		}
		if (intervaltime >= chancetime)
		{
			ds_challenge.challengenum += intervaltime / chancetime;
			if (ds_challenge.challengenum >= 3)
			{
				ds_challenge.challengenum = 3;
				ds_challenge.challengetime = DateTime.Now.Ticks.ToString();
				timelabel.text = "挑战次数";
				numlabel.text = "3/3";
			}
			else
			{
				ds_challenge.challengetime = (DateTime.Now.Ticks - (long)intervaltime % (long)chancetime * 10000000).ToString();
				intervaltime = chancetime - intervaltime % chancetime;
				numlabel.text = ds_challenge.challengenum + "/3";
				timelabel.text = "恢复时间 \n" + MyMath.longtimeset(intervaltime);
				showtime = true;
			}
			MenuPlayerprefs.instance.saveChallenge(ID - 6, ds_challenge);
		}
		else
		{
			intervaltime = chancetime - intervaltime % chancetime;
			numlabel.text = ds_challenge.challengenum + "/3";
			timelabel.text = "恢复时间 \n" + MyMath.longtimeset(intervaltime);
			showtime = true;
		}
	}

	private void Update()
	{
		if (!showtime)
		{
			return;
		}
		caltime += Time.deltaTime;
		if (!(caltime >= 1f))
		{
			return;
		}
		caltime = 0f;
		intervaltime--;
		if (intervaltime <= 0)
		{
			ds_challenge.challengenum++;
			if (ds_challenge.challengenum >= 3)
			{
				timelabel.text = "挑战次数";
				numlabel.text = "3/3";
				showtime = false;
			}
			else
			{
				timelabel.text = "恢复时间 \n" + MyMath.longtimeset(intervaltime);
				numlabel.text = ds_challenge.challengenum + "/3";
				intervaltime = chancetime;
				showtime = true;
			}
			ds_challenge.challengetime = DateTime.Now.Ticks.ToString();
			MenuPlayerprefs.instance.saveChallenge(ID - 6, ds_challenge);
		}
		else
		{
			timelabel.text = "恢复时间 \n" + MyMath.longtimeset(intervaltime);
		}
	}

	public void showtimefun()
	{
		timelabel.text = "恢复时间 \n";
		numlabel.text = ds_challenge.challengenum + "/3";
	}

	public void clickmodeBtn(GameObject btn)
	{
		switch (btn.name)
		{
			case "easy":
				modeclick(0);
				break;
			case "medium":
				modeclick(1);
				break;
			case "hard":
				modeclick(2);
				break;
		}
	}

	public void modeclick(int mode)
	{
		if (challdata[mode].openlevel <= curlevel)
		{
			if (MenuPlayerprefs.instance.DecEnergy(challdata[mode].energy))
			{
				MenuPlayerprefs.instance.setchall_hint(hintindex + mode);
				if (backhint[mode].enabled)
				{
					backhint[mode].enabled = false;
				}
				if (ds_challenge.challengenum == 3)
				{
					ds_challenge.challengetime = DateTime.Now.Ticks.ToString();
				}
				ds_challenge.challengenum--;
				MenuPlayerprefs.instance.saveChallenge(ID - 6, ds_challenge);
				Platform.flurryEvent_challenge(ID - 5, mode, 0);
				GlobalInf.gamemode = TASKMODE.chanllenge;
				GlobalInf.chanllengeid = ID;
				GlobalInf.chanllenge_mode = mode;
				MenuUIController.instance.nextscene();
			}
			else
			{
				MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
				MenuUIController.instance.enable_chargeui(2);
			}
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
			MenuUIController.instance.enable_hitinfoui(hitinfoid.challengelevel);
		}
	}

	public void ClickPicBtn(GameObject btn)
	{
		if (curlevel >= challdata[0].openlevel)
		{
			if (ds_challenge.challengenum > 0)
			{
				MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
				animaku.PlayForward();
				Invoke("showarea2", 0.25f);
			}
			else
			{
				MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
				MenuUIController.instance.enable_hitinfoui(hitinfoid.challengetimes);
			}
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
			MenuUIController.instance.enable_hitinfoui(hitinfoid.challengelevel);
		}
	}

	public void ClickcloseBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		animaku.PlayReverse();
		Invoke("showarea1", 0.25f);
	}

	public void showarea1()
	{
		midtra.localRotation = Quaternion.Euler(0f, 0f, 0f);
		area1wi.alpha = 1f;
		area1wi.gameObject.SetActive(true);
		area2wi.alpha = 0f;
		area2wi.gameObject.SetActive(false);
	}

	public void showarea2()
	{
		midtra.localRotation = Quaternion.Euler(0f, 180f, 0f);
		area1wi.alpha = 0f;
		area1wi.gameObject.SetActive(false);
		area2wi.alpha = 1f;
		area2wi.gameObject.SetActive(true);
	}

	public void ClickhelpBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		MenuUIController.instance.enable_challinfoui(ID);
	}

	private void OnApplicationFocus(bool isfocus)
	{
		if (isfocus)
		{
			if (islostfocus)
			{
				enable();
			}
		}
		else
		{
			islostfocus = true;
		}
	}
}
